<template>
  <div id="main_div">
    <div class="center-container">
      <el-card style="width: 500px;height: 400px;" v-show="!showRegisterForm">
        <el-form label-width="100px">
          <el-form-item label="用户名：">
            <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码：">
            <el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <span>
              <a href="#" @click="toggleRegisterForm">没有账号？请先注册</a>
            </span>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card style="width: 500px;height: 400px;" v-show="showRegisterForm">
        <el-form label-width="100px">
          <el-form-item label="用户名：">
            <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码：">
            <el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item label="手机号：">
            <el-input placeholder="请输入手机号" v-model="user.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register">注册</el-button>
            <span>
              <a href="#" @click="toggleRegisterForm">已有账号？请先登录</a>
            </span>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import axios from "axios";
import qs from 'qs';
import { ElMessage } from "element-plus";

export default {
  name: "LoginForm",
  setup() {
    const user = ref({ username: "", password: "", phone: "" });
    const showRegisterForm = ref(false);

    const login = () => {
      let data = qs.stringify(user.value);
      axios.post('http://localhost:8080/v1/users/login', data)
          .then(response => {
            if (response.data.code == 2001) {
              let user = response.data.data;
              ElMessage.success("登录成功！欢迎" + user.nickname + "回来！");
              localStorage.setItem('user', JSON.stringify(user));
              //router.push('/');
              location.href = '/';
            } else {
              ElMessage.error(response.data.msg);
            }
          });
    };

    const toggleRegisterForm = () => {
      showRegisterForm.value = !showRegisterForm.value;
    };

    const register = () => {
      let data = qs.stringify(user.value);
      axios.post('http://localhost:8080/v1/users/register', data)
          .then(response => {
            if (response.data.code == 2001) {
              let user = response.data.data;
              ElMessage.success("注册成功！欢迎" + user.nickname + "加入！");
              localStorage.setItem('user', JSON.stringify(user));
              //router.push('/');
              location.href = '/';
            } else {
              ElMessage.error(response.data.msg);
            }
          });
    };

    return {
      user,
      showRegisterForm,
      login,
      toggleRegisterForm,
      register
    };
  }
};
</script>

<style scoped>
#main_div {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("/public/imgs/bj.jpg");
  background-size: cover;
  background-position: center;
}

.center-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
</style>
